package com.usian.controller;

import com.usian.pojo.Student;
import com.usian.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.xmlunit.util.Mapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RequestMapping("/student")
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;


    @RequestMapping("/toadd")
    public String toadd(){
        return "add";
    }

    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<Student> list = studentService.findAll();
        model.addAttribute("list",list);
        return "index";
    }

    @RequestMapping("/addStu")
    public String addStu(/*HttpServletRequest request, HttpServletResponse response ,*/ Student student){
        studentService.addStu(student);
     /*   try {
            response.sendRedirect(request.getContextPath()+"findAll");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "index";*/
        return "redirect:/student/findAll";
    }

    @RequestMapping("/delStu")
    public String delStu(/*HttpServletRequest request, HttpServletResponse response , */Integer id){
        studentService.delStu(id);
      /*  try {
            response.sendRedirect(request.getContextPath()+"findAll");
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        return "redirect:/student/findAll";
    }

    @RequestMapping("/toupdate")
    public String toupdate(Integer id,Model model){
        Student student = studentService.findById(id);
        model.addAttribute("student",student);
        return "update";
      /*  ModelAndView view = new ModelAndView();
        view.addObject("student",student);
        return view;*/
    }

    @RequestMapping("/updateStu")
    public String updateStu(/*HttpServletRequest request, HttpServletResponse response ,*/Student student){
        studentService.updateStu(student);
       /* try {
            response.sendRedirect(request.getContextPath()+"findAll");
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        return "redirect:/student/findAll";
    }
}
